home *** CD-ROM | disk | FTP | other *** search
/ Champak 62 / Volume 62 - JOGO DISK .iso / Games / penguin_dinner.swf / scripts / __Packages / classes / math / CatmullRomSpline.as < prev    next >
Text File  |  2008-03-17  |  4KB  |  122 lines

  1. class classes.math.CatmullRomSpline
  2. {
  3.    var numVertices = 0;
  4.    var APPROX_LINE_LENGTH = 5;
  5.    function CatmullRomSpline(xVals, yVals)
  6.    {
  7.       this.xSamples = xVals;
  8.       this.ySamples = yVals;
  9.       this.numVertices = this.xSamples.length;
  10.       this.tSamples = new Array(this.numVertices);
  11.    }
  12.    function catmullRom(t, P0, P1, P2, P3)
  13.    {
  14.       var _loc5_ = t * t;
  15.       var _loc7_ = t * _loc5_;
  16.       return {x:0.5 * (2 * P1.x + (- P0.x + P2.x) * t + (2 * P0.x - 5 * P1.x + 4 * P2.x - P3.x) * _loc5_ + (- P0.x + 3 * P1.x - 3 * P2.x + P3.x) * _loc7_),y:0.5 * (2 * P1.y + (- P0.y + P2.y) * t + (2 * P0.y - 5 * P1.y + 4 * P2.y - P3.y) * _loc5_ + (- P0.y + 3 * P1.y - 3 * P2.y + P3.y) * _loc7_)};
  17.    }
  18.    function getAllPoints(approxLineLength)
  19.    {
  20.       if(approxLineLength == undefined)
  21.       {
  22.          approxLineLength = this.APPROX_LINE_LENGTH;
  23.       }
  24.       if(this.verticesPlus == null)
  25.       {
  26.          this.makeVerticesPlus();
  27.       }
  28.       if(this.lengths == null)
  29.       {
  30.          this.getSegmentLengths();
  31.       }
  32.       var _loc4_ = this.verticesPlus;
  33.       var _loc8_ = new Array();
  34.       var _loc9_ = 0;
  35.       var _loc2_ = undefined;
  36.       var _loc16_ = _loc4_.length - 3;
  37.       _loc2_ = 0;
  38.       while(_loc2_ < _loc16_)
  39.       {
  40.          var _loc6_ = _loc4_[_loc2_ + 1];
  41.          var _loc5_ = _loc4_[_loc2_ + 2];
  42.          var _loc7_ = this.catmullRom(0.5,_loc4_[_loc2_],_loc6_,_loc5_,_loc4_[_loc2_ + 3]);
  43.          var _loc14_ = _loc7_.x - _loc6_.x;
  44.          var _loc12_ = _loc7_.y - _loc6_.y;
  45.          var _loc13_ = _loc7_.x - _loc5_.x;
  46.          var _loc11_ = _loc7_.y - _loc5_.y;
  47.          var _loc15_ = Math.sqrt(_loc14_ * _loc14_ + _loc12_ * _loc12_) + Math.sqrt(_loc13_ * _loc13_ + _loc11_ * _loc11_);
  48.          this.lengths[_loc2_] = _loc15_;
  49.          var _loc10_ = approxLineLength / _loc15_;
  50.          var _loc3_ = 0;
  51.          while(_loc3_ < 1)
  52.          {
  53.             _loc8_[_loc9_] = this.catmullRom(_loc3_,_loc4_[_loc2_],_loc6_,_loc5_,_loc4_[_loc2_ + 3]);
  54.             _loc9_;
  55.             _loc8_[_loc9_++].vertex = _loc2_;
  56.             _loc3_ += _loc10_;
  57.          }
  58.          _loc2_ = _loc2_ + 1;
  59.       }
  60.       _loc9_;
  61.       _loc8_[_loc9_++] = this.catmullRom(1,_loc4_[_loc2_ - 1],_loc4_[_loc2_],_loc4_[_loc2_ + 1],_loc4_[_loc2_ + 2]);
  62.       return _loc8_;
  63.    }
  64.    function getTOfVertex(index)
  65.    {
  66.       if(this.lengths == null)
  67.       {
  68.          this.getSegmentLengths();
  69.       }
  70.       return this.tSamples[index];
  71.    }
  72.    function getSegmentLengths()
  73.    {
  74.       if(this.verticesPlus == null)
  75.       {
  76.          this.makeVerticesPlus();
  77.       }
  78.       var _loc7_ = this.verticesPlus;
  79.       this.lengths = new Array();
  80.       this.totalLength = 0;
  81.       var _loc14_ = _loc7_.length - 3;
  82.       var _loc2_ = 0;
  83.       while(_loc2_ < _loc14_)
  84.       {
  85.          var _loc6_ = _loc7_[_loc2_ + 1];
  86.          var _loc5_ = _loc7_[_loc2_ + 2];
  87.          var _loc4_ = this.catmullRom(0.5,_loc7_[_loc2_],_loc6_,_loc5_,_loc7_[_loc2_ + 3]);
  88.          var _loc11_ = _loc4_.x - _loc6_.x;
  89.          var _loc9_ = _loc4_.y - _loc6_.y;
  90.          var _loc10_ = _loc4_.x - _loc5_.x;
  91.          var _loc8_ = _loc4_.y - _loc5_.y;
  92.          var _loc12_ = Math.sqrt(_loc11_ * _loc11_ + _loc9_ * _loc9_) + Math.sqrt(_loc10_ * _loc10_ + _loc8_ * _loc8_);
  93.          this.lengths[_loc2_] = _loc12_;
  94.          this.totalLength += _loc12_;
  95.          _loc2_ = _loc2_ + 1;
  96.       }
  97.       this.tSamples[0] = 0;
  98.       var _loc13_ = 0;
  99.       var _loc15_ = this.lengths.length;
  100.       var _loc3_ = 0;
  101.       while(_loc3_ < _loc15_)
  102.       {
  103.          _loc13_ += this.lengths[_loc3_];
  104.          this.tSamples[_loc3_ + 1] = _loc13_ / this.totalLength;
  105.          _loc3_ = _loc3_ + 1;
  106.       }
  107.    }
  108.    function makeVerticesPlus()
  109.    {
  110.       this.verticesPlus = new Array();
  111.       var _loc5_ = this.xSamples.length;
  112.       var _loc2_ = 0;
  113.       while(_loc2_ < _loc5_)
  114.       {
  115.          this.verticesPlus.push({x:this.xSamples[_loc2_],y:this.ySamples[_loc2_]});
  116.          _loc2_ = _loc2_ + 1;
  117.       }
  118.       this.verticesPlus.splice(0,0,this.verticesPlus[0]);
  119.       this.verticesPlus.push(this.verticesPlus[this.verticesPlus.length - 1]);
  120.    }
  121. }
  122.